<script type="text/javascript">/*<![CDATA[*/
var originalApplication = new Object();
var _xforms_focusElements = [];
var polygonDetailsModel;
var locationDetailsModel;
var directionDetailsModel;
var DataSourceListModel;
_xforms_focusElements.push('NewImage');
var _webform_asyncLibCount=0;var _webform_numAsyncLibCount=0;var _webform_asynlibcache = [];
_webform_asyncload();
function importFormRunner()
{
	var _logger = application.getLogger(); if (_logger) _logger.setDiagnosticContext('XForms','/uploadpicture.caf');
	var wcpSession;if (window == top && (wcpSession = application.getParameter('wcp-session'))){window.system.parameters['wcp-session'] = wcpSession;}
	_addLibraries();
	system.addLibraryPackage('/cordys/cas/xforms/scripthostlibrary/formrunnerlibraries.htm');
	application.addType(window, "cas.xforms.scripthostlibrary.FormRunner");
	_webform_applicationReady();
}
function _webform_initializeForm()
{
	var DataSourceListModelIndex = new Array();
	var DataSourceListModelinstance = WebForm.getInstance(DataSourceListModelIndex,"","DataSourceList", application, false);
	DataSourceListModel = WebForm.getModel(DataSourceListModelinstance,"DataSourceListModel", null, true);
	DataSourceListModel.instance.setTimeout();
	DataSourceListModel.automatic = false;
	DataSourceListModel.promptToSave = false;
	DataSourceListModel.setGateway('com.eibus.web.soap.Gateway.wcp');
	DataSourceListModel._bind = {};
	var directionDetailsModelIndex = new Array();
	var directionDetailsModelinstance = WebForm.getInstance(directionDetailsModelIndex,"","directionDetails", application, false);
	directionDetailsModel = WebForm.getModel(directionDetailsModelinstance,"directionDetailsModel", null, true);
	directionDetailsModel.instance.setTimeout();
	directionDetailsModel.automatic = false;
	directionDetailsModel.promptToSave = false;
	directionDetailsModel.setGateway('com.eibus.web.soap.Gateway.wcp');
	directionDetailsModel._bind = {};
	var locationDetailsModelIndex = new Array();
	var locationDetailsModelinstance = WebForm.getInstance(locationDetailsModelIndex,"","LocationDetails", application, false);
	locationDetailsModel = WebForm.getModel(locationDetailsModelinstance,"locationDetailsModel", null, true);
	locationDetailsModel.instance.setTimeout();
	locationDetailsModel.automatic = false;
	locationDetailsModel.promptToSave = false;
	locationDetailsModel.setGateway('com.eibus.web.soap.Gateway.wcp');
	locationDetailsModel._bind = {};
	var polygonDetailsModelIndex = new Array();
	var polygonDetailsModelinstance = WebForm.getInstance(polygonDetailsModelIndex,"","polygonDetails", application, false);
	polygonDetailsModel = WebForm.getModel(polygonDetailsModelinstance,"polygonDetailsModel", null, true);
	polygonDetailsModel.instance.setTimeout();
	polygonDetailsModel.automatic = false;
	polygonDetailsModel.promptToSave = false;
	polygonDetailsModel.setGateway('com.eibus.web.soap.Gateway.wcp');
	polygonDetailsModel._bind = {};
}
function _webform_default_initialize_doneHandler()
{
	polygonDetailsModel.feedProcessData();
	locationDetailsModel.feedProcessData();
	directionDetailsModel.feedProcessData();
	DataSourceListModel.feedProcessData();
}
function _webform_beforeclose_handler()
{
}
function _addLibraries()
{
}
var _webform_EventHandlers = new Array();
_webform_EventHandlers['xforms_initialize_done'] = new Array(Form_InitDone);
var _webform_EBIModels = new Array();

var gDataExchange = new Object();
var namespacearray = {'ns1':"http://schemas.cordys.com/bsf/uc/webapps/1.0/ucappsutil", 'soap':"http://schemas.xmlsoap.org/soap/envelope/"};

function Form_InitDone(eventObject)
{debugger;
    gDataExchange = application.event.data;
    loc_browse.innerHTML = "<form xmlns='' action='com.eibus.web.tools.upload.Upload.wcp' id='browseButton' name='uploadpicture' style='float:right' enctype='multipart/form-data' method='post'><label id='loc_uploadLabel' class='cabinet'><input id='upload' type='file' size='1' width='0px' name='file1' class='file' onchange='btnBrowse_Click()'/></label></form>";
	document.getElementById("loc_uploadLabel").style.backgroundImage = "url("+translate("/cordys/uc/webapps/images/btn-choose-file.gif?version=r39347")+")";
	document.getElementById("text4").innerHTML = "<sup>*</sup> "+translate("Required");
	document.getElementById("rte1").innerHTML = "<P>"+translate("Existing Image:")+"</P>";
    var lblElement = document.getElementById("NewImage").parentNode.parentNode.childNodes[0];
	lblElement.innerHTML = lblElement.innerHTML + " <sup>*</sup>";
	oldPic.src=gDataExchange.oldPic;
    var imageElement=new Image();
    imageElement.src=gDataExchange.oldPic;
    initialWidth=100;
    initialHeight=100;
if(gDataExchange.oldPic=="")
{
 oldPic.src = "/cordys/uc/webapps/images/unselected.gif?version=r39347";
return; 
 }
window.setTimeout(function(){resizePic(imageElement,oldPic,initialWidth,initialHeight)},200);
}

function resizePic(imageElement,oldPic,initialWidth,initialHeight)
{
imageElement.height=gDataExchange.oldPictureHeight;
imageElement.width=gDataExchange.oldPictureWidth;
if((imageElement.width < initialWidth) &&(imageElement.height < initialHeight)) 
    {
    oldPic.style.height = imageElement.height+"px";
    oldPic.style.width = imageElement.width+"px";
    return;
    } 
      if((initialWidth/initialHeight) == (imageElement.width/imageElement.height))
        {
        oldPic.style.height = initialHeight;//fix height; 
        oldPic.style.width = initialWidth;//fix width;
        }
        else if((initialWidth/initialHeight) > (imageElement.width/imageElement.height))
        {
        //fix height; 
         oldPic.style.height = initialHeight;
        oldPic.style.width = Math.floor((imageElement.width*initialHeight)/imageElement.height)+"px";
        
        }
        else
        {
        //fix width;
        oldPic.style.width = initialWidth;
         oldPic.style.height = Math.floor((imageElement.height*initialWidth)/imageElement.width)+"px"; 
        } 
 }
  
function btnBrowse_Click()
{  debugger;
    var gFileChosen = true;
    var fileExtension = "";
    try
    {
        var filePath =document.getElementById('upload').value;  
        var fileExtension = filePath.substring(filePath.lastIndexOf(".")+1).toLowerCase();
        var gFileName = filePath.substring(filePath.lastIndexOf("\\")+1,filePath.length);
        if(fileExtension != "")
        {
            if(fileExtension == 'gif' || fileExtension == 'jpg' || fileExtension == 'jpeg'|| fileExtension == 'tif'|| fileExtension == 'png' || fileExtension == 'bmp' )
            {
                gUploadFilePath = filePath;
                document.getElementById("NewImage").setValue(gFileName) ;
            }
            else
            {
               application.notify(translate("Please select a image file only"));
                filePath = "";
                return;
            }
        }
        filePath  = "";
        fileExtension = "";
    }
    catch(error)
    {
        
        return null;
    
    }
}  
function UploadButton_Click(eventObject)
{debugger;
    if(document.getElementById('upload').value)
   {
    gDataExchange.picturePath =document.getElementById('upload').value;
    pfileName=gDataExchange.picturePath;
    var orgDN = application.organization;
    var orgName = orgDN.substring(2,orgDN.indexOf(","));
    extension = pfileName.substring(pfileName.lastIndexOf(".")+1).toLowerCase(); 
    var componentPath = orgName+"/images/"+gDataExchange.appId+"/"+gDataExchange.boid+"/"+gDataExchange.fieldId;
        if(extension == 'gif' || extension == 'jpg' || extension == 'jpeg'|| extension == 'tif'|| extension == 'png' || extension == 'bmp' || extension == 'ico')
    {
    pfileName = pfileName.substring(pfileName.lastIndexOf("\\")+1,pfileName.lastIndexOf("."))+".pic";
    gDataExchange.pfileName =pfileName;
   
    var uploadFileNodeRequest = cordys.cloneXMLDocument(uploadFileNode.XMLDocument);
    if(gDataExchange.translation == true)
    {
        componentPath = orgName+"/"+gDataExchange.applicationId+"/Translation/Images/"+gDataExchange.labelId+"/"+gDataExchange.languageCode;
        cordys.setTextContent(cordys.selectXMLNode(uploadFileNodeRequest, "./soap:Envelope/soap:Body/ns1:UploadFile/ns1:type", namespacearray),"translation");
    }
    cordys.setTextContent(cordys.selectXMLNode(uploadFileNodeRequest, "./soap:Envelope/soap:Body/ns1:UploadFile/ns1:fileName ", namespacearray),pfileName)
    cordys.setTextContent(cordys.selectXMLNode(uploadFileNodeRequest, "./soap:Envelope/soap:Body/ns1:UploadFile/ns1:componentPath ", namespacearray),componentPath)
    gUpload = document.getElementById("UploadButton");
    application.addLibrary("/cordys/wcp/library/util/upload.htm",gUpload);
    gUpload.request = uploadFileNodeRequest;
    gUpload.organization = application.organization;
    gUpload.uploadFile(document.getElementById("browseButton"));
    gUpload.onupload = function(eventObject){runtime_uploadHandler(eventObject)};
    }    
    
  else{
application.notify(translate("Select an Image "));
      return false;
  }    
     
    }
    else
    {
application.notify(translate("Please Select Some Image "));
    }
}

function runtime_uploadHandler(eventObject)
{debugger;
 if(window.application.event.status){
  filepath = cordys.getNodeText(eventObject.response, "./soap:Body/ns1:UploadFileResponse/ns1:tuple/ns1:old/ns1:upload/ns1:upload/ns1:DeleteTempFile/ns1:FilePath", "",  namespacearray);
     if(gDataExchange.translation == true)
    {
        gDataExchange.filePath = filepath ;
    }
  filepath=filepath.substring(filepath.indexOf("uc/"),filepath.length);
  gDataExchange.pictGuid=filepath.substring(filepath.lastIndexOf("/")+1,filepath.lastIndexOf(".pic"));
  filepath=filepath.replace(".pic","/"+gDataExchange.pictGuid+".pic");


  gDataExchange.pfileName =gDataExchange.pictGuid;
   
  application.removeLibrary("/cordys/wcp/library/util/upload.htm",gUpload);
  application.container.close();
 }else{
  window.application.event.showError = false
        application.showError(cordys.getNodeText(window.application.event.error, "./faultstring"));
 }
}
function cancelButton_Click(eventObject)
{
    gDataExchange.picturePath="";
    gDataExchange.pfileName = "";
    application.container.close();
    gDataExchange.cancelled = true;
}

function dialogReturnValue()
{
    return gDataExchange;
}

var _xforms_messages = new Array();
function _loadCCLibraries()
{
}
function _compositeControlsDisableHide()
{
}

function _webform_downloadLibrary(url, isType)
{if ( isType )
{url = ('/cordys/'+ url.replace( /\./g, "/" )+'.htm').toLowerCase();}
try{if ( _webform_asynlibcache[ url ] || ( window.parent != window && window.parent.system && ( window.parent.system.containers[url] || window.parent.system.librarySources[url] || window.parent.system.libraries[url] ))){return;}
}
catch(e){}
_webform_asynlibcache[ url ] = url;var xmlHttpConnection = _webform_getXMLHTTPConnection();
xmlHttpConnection.onreadystatechange = function()
{
if ( xmlHttpConnection.readyState == 4 )
{
_webform_asyncLibCount++;
if ( _webform_asyncLibCount == _webform_numAsyncLibCount && typeof( _webform_loadApplicationJS ) == 'function' ) _webform_loadApplicationJS();
}
}
xmlHttpConnection.open('GET', url, true);
xmlHttpConnection.send(null);
 }
function _webform_getXMLHTTPConnection()
{
if ( window.XMLHttpRequest )
{
_webform_numAsyncLibCount++;
return new XMLHttpRequest();
}
else if ( window.ActiveXObject )
{
_webform_numAsyncLibCount++;
return new ActiveXObject('Microsoft.XMLHTTP');
}
else
{
return false;
}
}
var isIE = window.navigator.appVersion.match(/MSIE (\d+)/) != null;
if ( ! isIE )
{document.addEventListener('DOMContentLoaded', function()
{
this.removeEventListener('DOMContentLoaded',arguments.callee,false);
this._readyState='complete';
},false);
}
function _webform_asyncload()
{
	var isCrossdomain = false;
	try{ var c1 = window.parent.document._undefined; }
	catch(e){ isCrossdomain = true; }
	if ( isCrossdomain || window.parent == window || ! window.parent.system ){
	_webform_downloadLibrary('/cordys/wcp/libraries.htm');
	_webform_downloadLibrary('/cordys/cas/xforms/scripthostlibrary/formrunnerlibraries.htm');
	return;}
	if ( _webform_numAsyncLibCount == 0 && typeof( _webform_loadApplicationJS ) == 'function') _webform_loadApplicationJS();
}
/*]]>*/</script>